package cn.piesat.scanning.utils;

import redis.clients.jedis.Jedis;

/**
 * redis 任务统记录redis工具类
 * @author liuyang
 */
public class TaskRecordUtils {

    private static final String BASE_NAME = "AutoTask:";

    private TaskRecordUtils(){}

    /**
     * 获取 redis存储中 hkey
     * @param taskId 任务Id
     */
    public static String getHkey(String taskId){
        return BASE_NAME + taskId;
    }

    /**
     * 等待计数操作
     * @param hkey 任务计数存储key
     * @param number 变量（正数加法,负数减法）
     */
    public static void increaseWaitCount(String hkey,long number){
        Jedis jedis = RedisUtil.getJedis();
        if (jedis != null) {
            boolean exists = jedis.exists(hkey);
            if (!exists) {
                initRecordMap(hkey);
            }
            long l = Long.parseLong(jedis.hget(hkey, MapField.WAIT.getField()));
            if (l + number < 0) {
                jedis.hset(hkey, MapField.WAIT.getField(), String.valueOf(0));
            } else {
                jedis.hincrBy(hkey, MapField.WAIT.getField(), number);
            }

        }
        RedisUtil.close(jedis);
    }

    /**
     * 运行计数操作
     * @param hkey 任务计数存储key
     * @param number 变量（正数加法,负数减法）
     */
    public static void increaseRunningCount(String hkey,long number){
        Jedis jedis = RedisUtil.getJedis();
        if (jedis != null) {
            boolean exists = jedis.exists(hkey);
            if (exists) {
                long i = Long.parseLong(jedis.hget(hkey, MapField.RUNNING.getField()));
                if (i + number < 0) {
                    jedis.hset(hkey, MapField.RUNNING.getField(), String.valueOf(0));
                } else {
                    jedis.hincrBy(hkey, MapField.RUNNING.getField(), number);
                }
            }
        }
        RedisUtil.close(jedis);
    }

    /**
     * 错误计数操作
     * @param hkey 任务计数存储key
     * @param number 变量（正数加法,负数减法）
     */
    public static void increaseFailCount(String hkey,long number){
        Jedis jedis = RedisUtil.getJedis();
        if (jedis != null) {
            boolean exists = jedis.exists(hkey);
            if (exists) {
                long i = Long.parseLong(jedis.hget(hkey, MapField.FAIL.getField()));
                if (i + number < 0) {
                    jedis.hset(hkey, MapField.FAIL.getField(), String.valueOf(0));
                } else {
                    jedis.hincrBy(hkey, MapField.FAIL.getField(), number);
                }
            }
        }
        RedisUtil.close(jedis);
    }

    /**
     * 初始化存储对象
     * @param hkey 任务计数存储key
     */
    private static void initRecordMap(String hkey){
        Jedis jedis = RedisUtil.getJedis();
        if(jedis!=null){
            jedis.hsetnx(hkey, MapField.WAIT.field,String.valueOf(0));
            jedis.hsetnx(hkey, MapField.RUNNING.field,String.valueOf(0));
            jedis.hsetnx(hkey, MapField.FAIL.field,String.valueOf(0));
        }
        RedisUtil.close(jedis);
    }

    /**
     * map field
     */
    public enum MapField{
        //等待计数
        WAIT("WaitCount"),
        //运行计数
        RUNNING("RunningCount"),
        //失败计数
        FAIL("FailCount");
        private final String field;

        MapField(String field){
            this.field = field;
        }

        public String getField(){
            return field;
        }
    }
}
